﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class ReceiptDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitializeFormData();
            }
        }

        private void InitializeFormData()
        {
            ApplicationService applicationService = new ApplicationService();
            ApplicationForm applicationForm = applicationService.GetApplicationFormByID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
            rmaCodeLabel.Text = applicationForm.ApplicationCode;
            runningNOLbl.Text = applicationForm.RunningNumber;
            if (!applicationForm.UserReference.IsLoaded)
            {
                applicationForm.UserReference.Load();
            }
            if (!applicationForm.GatheringReference.IsLoaded)
            {
                applicationForm.GatheringReference.Load();
            }
            companyNameLabel.Text = applicationForm.User.CompanyName;
            quatationDateTimeLbl.Text = applicationForm.QuatationDatetime.HasValue? applicationForm.QuatationDatetime.Value.ToString("yyyy-MM-dd") : "";
            companyAddressLbl.Text = applicationForm.User.UserAddress;
            receiveMoneyDateTimeLbl.Text = applicationForm.ReceiveDateTime.HasValue ? applicationForm.Gathering.PayDateTime.Value.ToString("yyyy-MM-dd") : "";
            connectPersonLabel.Text = applicationForm.User.ContactPerson;
            faxLbl.Text = applicationForm.User.UserFax;
            phoneNumberLabel.Text = applicationForm.User.UserPhone;
            emailLbl.Text = applicationForm.User.UserEmail;

            if (!applicationForm.GatheringReference.IsLoaded)
            {
                applicationForm.GatheringReference.Load();
            }

            hfRealpay.Value = applicationForm.Gathering.RealPay.HasValue ? applicationForm.Gathering.RealPay.Value.ToString() : "0";
            hfUserName.Value = applicationForm.User.UserName;
            mailAddressLabel.Text = applicationForm.User.UserEmail;
            mailConnectPersonLabel.Text = applicationForm.User.ContactPerson;

            ApplicationProductService productService = new ApplicationProductService();
            List<ApplicationProduct> products = productService.GetApplicatonProductByFormID(applicationForm.ApplicationID);
            productGridView.DataSource = products.Where(p=>p.IsDecideToRepair.HasValue&&p.IsDecideToRepair.Value==true&&p.IsQuotePrice.HasValue&&p.IsQuotePrice.Value==true);
            productGridView.DataBind();

            DropDownItemService dropDownItemService = new DropDownItemService();
            List<DropDownItem> receiptList = dropDownItemService.GetAllReceiptType();
            foreach (GridViewRow row in productGridView.Rows)
            {
                DropDownList kpType = (DropDownList)row.FindControl("KPTypeDDL");
                kpType.Items.Clear();
                foreach (DropDownItem receipt in receiptList)
                {
                    kpType.Items.Add(new ListItem(receipt.Name, receipt.Name));
                }
            }
        }

        protected void productGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        // 点击发送按钮事件处理程序，打开发送对话框
        protected void mailLinkButton_Click(object sender, EventArgs e)
        {
            RMALbl.Text = rmaCodeLabel.Text;
            bodyTextBox.Text = string.Format(WebConfigurationManager.AppSettings["ConfirmGatheringEmail"], mailConnectPersonLabel.Text, rmaCodeLabel.Text, DateTime.Now.ToString("yyyy-MM-dd"), hfRealpay.Value);
            ModalPopupExtender1.Show();    
        }
        // 点击发送对话框上的发送按钮事件处理程序
        protected void sendEmailLinkButton_Click(object sender, EventArgs e)
        {
            EmailService.SendEmail(mailAddressLabel.Text, "神州数码客服中心发票速递通知", bodyTextBox.Text);
            submitLinkButton.Enabled = true;
        }

        // 点击提交按钮事件处理程序
        protected void submitLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                ApplicationService applicationService = new ApplicationService();
                ApplicationForm applicationForm = applicationService.GetApplicationFormByID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
                applicationForm.KPDateTime = DateTime.Now;
                applicationService.UpdateApplication(applicationForm);

                FPSDService fpsdService = new FPSDService();
                FPSDRecord fpsdRecord = new FPSDRecord { FPID = fphTextBox.Text, FHDID = fhdhTextBox.Text, Mark = bzTextBox.Text, CreateDate = DateTime.Now };

                ApplicationProductService productService = new ApplicationProductService();
                foreach (GridViewRow row in productGridView.Rows)
                {
                    long productID = Convert.ToInt64(((Label)row.FindControl("ProductIDLbl")).Text);
                    string kpType = ((DropDownList)row.FindControl("KPTypeDDL")).SelectedValue;
                    ApplicationProduct product = productService.GetApplicationProductByID(productID);
                    product.KPType = kpType;
                    productService.UpdateProduct(product);
                    fpsdRecord.ApplicationProduct = product;
                }

                long fpsdID = fpsdService.AddFPSD(fpsdRecord);

                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('信息已成功保存。');", true);
                Response.Redirect("ReceiptExpressDeliveryConfirm.aspx");
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ReceiptDetail.aspx::submitLinkButton_Click");
                throw ex;
            }
        }

        protected void backLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("ReceiptExpressDeliveryConfirm.aspx");
        }
    }
}
